﻿/*****************************************************************************  

 Version: 0.1
 Website: http://www.visuagile.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/

#region Imports

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using VisualAgile.IBLL;
using VisualAgile.IDAL;
using VisualAgile.Model;

#endregion


namespace VisualAgile.BLL
{
    public abstract class BaseManager<EntityT> : IBaseManager<EntityT>
    {
        #region Properties

        public IBaseDao<EntityT> EntityDao { get; set; }

        #endregion        

        protected abstract IBaseDao<EntityT> getBaseDAO();
       
        /// <summary>
        /// Save entity
        /// </summary>
        /// <param name="entity">database table</param>
        /// <returns></returns>
        public EntityT Save(EntityT entity)
        {            
            Validate(entity);
            //if (Get(entity.Name) != null)
            //    throw new  Exception("Existing entity");
            return this.getBaseDAO().Save(entity);
        }

        /// <summary>
        /// Get entity
        /// </summary>
        /// <param name="userId">Entity ID</param>
        /// <returns></returns>
        public EntityT Get(Guid Id)
        {
            return this.getBaseDAO().FindById(Id);
        }

        /// <summary>
        /// Delete entity
        /// </summary>
        /// <param name="userId">Entity ID</param>
        /// <returns></returns>
        public void Delete(Guid Id)
        {
            var entity = Get(Id);
            this.getBaseDAO().Delete(entity);
        }

        /// <summary>
        /// Update entity
        /// </summary>
        /// <param name="entity">entity</param>
        /// <returns></returns>
        public EntityT Update(EntityT entity)
        {
            return this.getBaseDAO().SaveOrUpdate(entity);
        }


        /// <summary>
        /// Get ALL
        /// </summary>
        /// <returns></returns>
        public  IList GetAll()
        {
            return this.getBaseDAO().FindAll();
        }

        private void Validate(EntityT entity)
        {
            //TODO throw exception on error.
        }
    }
}
